enum MODEL{
    //%block="风向"
    WindDirection,
    //%block="平均风速"
    WindSpeedAverage,
    //%block="最大风速"
    WindSpeedMax,
    //%block="一小时内下雨量"
    RainfallOneHour,
    //%block="一天内下雨量"
    RainfallOneDay,
    //%block="温度"
    Temperature,
    //%block="湿度"
    Humidity,
    //%block="压强"
    BarPressure

}

//% color="#00BFFF" iconWidth=50 iconHeight=40
namespace Weather{
    //% block="刷新气象站数据" blockType="command"
    export function WeatherInit(parameter: any, block: any) {
        Generator.addInclude('weather1', 'char databuffer[35];');
        Generator.addInclude('weather2', 'double temp;');
        Generator.addObject(`weather3`, `void`, `getBuffer()                                                                    //Get weather status data
        {
          int index;
          for (index = 0;index < 35;index ++)
          {
            if(Serial.available())
            {
              databuffer[index] = Serial.read();
              if (databuffer[0] != 'c')
              {
                index = -1;
              }
            }
            else
            {
              index --;
            }
          }
        }`);
        Generator.addObject(`weather4`, `int`, `transCharToInt(char *_buffer,int _start,int _stop)                               //char to int）
        {
          int _index;
          int result = 0;
          int num = _stop - _start + 1;
          int _temp[num];
          for (_index = _start;_index <= _stop;_index ++)
          {
            _temp[_index - _start] = _buffer[_index] - '0';
            result = 10*result + _temp[_index - _start];
          }
          return result;
        }`);
        Generator.addObject(`weather5`, `int`, `WindDirection()                                                                  //Wind Direction
        {
          return transCharToInt(databuffer,1,3);
        };`);
        Generator.addObject(`weather6`, `float`, `WindSpeedAverage()                                                             //air Speed (1 minute)
        {
          temp = 0.44704 * transCharToInt(databuffer,5,7);
          return temp;
        }`);
        Generator.addObject(`weather7`, `float`, `WindSpeedMax()                                                                 //Max air speed (5 minutes)
        {
          temp = 0.44704 * transCharToInt(databuffer,9,11);
          return temp;
        }`);
        Generator.addObject(`weather8`, `float`, `Temperature()                                                                  //Temperature ("C")
        {
          temp = (transCharToInt(databuffer,13,15) - 32.00) * 5.00 / 9.00;
          return temp;
        }`);
        Generator.addObject(`weather9`, `float`, `RainfallOneHour()                                                              //Rainfall (1 hour)
        {
          temp = transCharToInt(databuffer,17,19) * 25.40 * 0.01;
          return temp;
        }`);
        Generator.addObject(`weather10`, `float`, `RainfallOneDay()                                                               //Rainfall (24 hours)
        {
          temp = transCharToInt(databuffer,21,23) * 25.40 * 0.01;
          return temp;
        }`);
        Generator.addObject(`weather11`, `int`, `Humidity()                                                                       //Humidity
        {
          return transCharToInt(databuffer,25,26);
        }`);
        Generator.addObject(`weather12`, `float`, `BarPressure()                                                                  //Barometric Pressure
        {
          temp = transCharToInt(databuffer,28,32);
          return temp / 10.00;
        }`);
        Generator.addSetup("aprs_begin", `Serial.begin(9600);`,true);
        Generator.addCode(`getBuffer();`);
    }
    
    //% block="获取气象站的[op]数据" blockType="reporter"
    //% op.shadow="dropdown" op.options="MODEL" op.defl="MODEL.温度"
    export function MPU6050ACCEL(parameter: any, block: any) {
        let Op = parameter.op.code;
        
        Generator.addCode(`${Op}()`);

    }

}
